#!/usr/bin/env node
const { Command } = require("commander");
const fs = require("fs");
const path = require("path");
const emoji = require("node-emoji");
const copyFolderSync = require(path.resolve(__dirname, "./copyFolderSync"));

const program = new Command();
const version = JSON.parse(
  fs.readFileSync(path.resolve(__dirname, "./package.json"), "utf-8")
).version;
const cwd = process.cwd();

program
  .usage("create-app <filename>")
  .name("yjrcli")
  .version(version, "-v", "显示版本号")
  .helpOption("-h", "查看帮助")
  .addHelpCommand(false)
  .command("create-app <filename>")
  .description("创建模板")
  .action(async (filename, options) => {
    if (!options.quiet) {
      console.log(
        emoji.emojify(
          `正在创建 ${filename} 请稍等... :hourglass_flowing_sand:\n`
        )
      );
    }
    console.time("耗时");
    copyFolderSync(
      path.resolve(__dirname, "./.template"),
      path.resolve(cwd, filename)
    );
    if (!options.quiet) {
      console.log(emoji.emojify("创建成功! :sparkles:"));
      console.timeEnd("耗时");
      console.log("");
    }
  });

// 处理选项和参数
program.parse(process.argv);

// 未输出任何参数时,打开帮助面板
if (!program.args.length) {
  program.help("");
}
